import {createRouter, createWebHashHistory} from "vue-router";
import {menuStore} from "@/store/menu";
import wechatRouter from "@/router/wechat";
import {commonStore} from "@/store/common";

const router = createRouter({
    history: createWebHashHistory('/'),
    routes: [
        {path: '/404', component: () => import('../views/error/404'), hidden: true},
        {path: '/403', component: () => import('../views/error/403'), hidden: true},
        {path: '/', redirect: '/login',},
        {
            path: '/login',
            component: () => import('../views/login.vue'),
            meta: {
                title: '登录',
            },
        },
        {
            path: '/register',
            component: () => import('../views/register.vue'),
            meta: {
                title: '注册',
            },
        },
        {
            path: '/main',
            component: () => import('../components/Main.vue'),
            children: [
                {
                    path: '/dashboard',
                    component: () => import('../views/dashboard.vue'),
                    meta: {
                        title: '工作台',
                    },
                },
                {
                    path: '/user',
                    component: () => import('../views/user/user.vue'),
                    meta: {
                        title: '用户管理',
                        BreadcrumbList: [{
                            path: '/user',
                            name: '用户管理'
                        }]
                    }
                },
                {
                    path: '/user/:id',
                    component: () => import('../views/user/userDetail.vue'),
                    props: true,
                    meta: {
                        title: '用户管理',
                        BreadcrumbList: [{
                            path: '/user',
                            name: '用户管理'
                        },
                            {
                                path: '/user/:id',
                                name: '用户详情'
                            }]
                    }
                },
                {
                    path: '/user/profile', component: () => import('../views/user/userProfile.vue'),
                    meta: {
                        title: '个人信息',
                        BreadcrumbList: [{
                            path: '/user/profile',
                            name: '个人信息'
                        }]
                    }
                },

                {
                    path: '/user/role/:username',
                    component: () => import('../views/user/userRole.vue'),
                    props: true,
                    meta: {
                        title: '分配角色',
                        BreadcrumbList: [{
                            path: '/user',
                            name: '用户管理'
                        },
                            {
                                path: '/user/role/:username',
                                name: '分配角色'
                            }]
                    }
                },
                {
                    path: '/department',
                    component: () => import('../views/department/department.vue'),
                    meta: {
                        title: '部门管理',
                        BreadcrumbList: [{
                            path: '/department',
                            name: '部门管理'
                        }]
                    }
                },
                {
                    path: '/department/:id/:parent?',
                    component: () => import('../views/department/departmentDetail.vue'),
                    props: true,
                    meta: {
                        title: '部门管理',
                        BreadcrumbList: [{
                            path: '/department',
                            name: '部门管理'
                        }, {
                            path: '/department/:id/:parent?',
                            name: '部门详情'
                        }]
                    }
                },
                {
                    path: '/role',
                    component: () => import('../views/role/role.vue'),
                    meta: {
                        title: '角色管理',
                        BreadcrumbList: [{
                            path: '/role',
                            name: '角色管理'
                        }]
                    }
                },
                {
                    path: '/permission',
                    component: () => import('../views/permission/permission.vue'),
                    meta: {
                        title: '权限管理',
                        BreadcrumbList: [{
                            path: '/permission',
                            name: '权限管理'
                        }]
                    }
                },
                {
                    path: '/permission/:id/:parent?',
                    component: () => import('../views/permission/permissionDetail.vue'),
                    props: true,
                    meta: {
                        title: '权限管理',
                        BreadcrumbList: [{
                            path: '/permission',
                            name: '权限管理'
                        }, {
                            path: '/permission/:id/:parent?',
                            name: '权限详情'
                        }]
                    }
                },
                {
                    path: '/dict',
                    component: () => import('../views/dict/dict.vue'),
                    meta: {
                        title: '字典管理',
                        BreadcrumbList: [{
                            path: '/dict',
                            name: '字典管理'
                        }]
                    }
                },
                {
                    path: '/dict/:code',
                    component: () => import('../views/dict/dictDetail.vue'),
                    props: true,
                    meta: {
                        title: '字典详情',
                        BreadcrumbList: [{
                            path: '/dict',
                            name: '字典管理'
                        }, {
                            path: '/dict/:code',
                            name: '字典详情'
                        }]
                    }
                },

                {
                    path: '/fileMapping', component: () => import('../views/filemapping/fileMapping'),
                    meta: {
                        title: '文件管理',
                        BreadcrumbList: [{
                            path: '/fileMapping',
                            name: '文件管理'
                        }]
                    }
                },
                {
                    path: '/fileMapping/new/:parent/:parentName',
                    component: () => import('../views/filemapping/fileMappingDetail.vue'),
                    props: true,
                    meta: {
                        title: '文件上传',
                        BreadcrumbList: [{
                            path: '/fileMapping',
                            name: '文件管理'
                        }, {
                            path: '/fileMapping/new/:parent/:parentName',
                            name: '文件上传'
                        }]
                    }
                },
                {
                    path: '/fileMapping/folder/new/:parent/:parentName',
                    component: () => import('../views/filemapping/fileMappingFolder.vue'),
                    props: true,
                    meta: {
                        title: '新建目录',
                        BreadcrumbList: [{
                            path: '/fileMapping',
                            name: '文件管理'
                        }, {
                            path: '/fileMapping/folder/new/:parent/:parentName',
                            name: '新建目录'
                        }]
                    }
                },
                {
                    path: '/fileMapping/folder/:id/:parent/:parentName?',
                    component: () => import('../views/filemapping/fileMappingFolder.vue'),
                    props: true,
                    meta: {
                        title: '文件目录',
                        BreadcrumbList: [{
                            path: '/fileMapping',
                            name: '文件管理'
                        }, {
                            path: '/fileMapping/folder/:id/:parent/:parentName?',
                            name: '文件目录'
                        }]
                    }
                },

                ...wechatRouter
            ]
        },
    ]
})

router.beforeEach((to, from, next) => {
    const menuS = menuStore()
    if (to.matched.length === 0) {
        next('/404'); // 判断此跳转路由的来源路由是否存在，存在的情况跳转到来源路由，否则跳转到404页面
    }
    if (to.matched[0].path === '/main') {
        if (to.meta && to.meta.BreadcrumbList && to.meta.BreadcrumbList.length > 1) {
            menuS.activeMenu = to.meta.BreadcrumbList[0].path;
        } else {
            menuS.activeMenu = to.path
        }
    }
    next() // 如果匹配到正确跳转
});


router.afterEach(to => {
    const commonStoreS = commonStore()

    // 更改标题
    if (to.meta.title) {
        document.title = commonStoreS.systemName + '-' + to.meta.title
    }
    // 返回页面顶端
    window.scrollTo(0, 0);
});

export default router
